﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iSun.Web.UI.Controls;
using iSun.T4Template;
using iSun.Common;
using iSun.Model;
using iSun.Entity;
using iSun.BLL;
using iSun.BLL.Base;

namespace iSun.Web.Forms
{
    public partial class MyForm : EditPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            _MasterEntity = GetDTEntity();
            CreateUI();
        }

        
        /// <summary>
        /// 生成界面及数据
        /// </summary>
        private void CreateUI()
        {
            // 生成UI
            string entity = GetStringFromQueryString("entity",""); // TODO: 初始化为适当的值
            UIConfig target = new UIConfig(entity,MasterEntity as DataTableBaseEntity);
            MasterHTML = target.TransformText();
            // 生成数据
            UIData _uiData = new UIData(entity,MasterEntity as DataTableBaseEntity);
            
            // 生成字典信息 
            JsonData = _uiData.TransformText();

        }

        private BaseEntity _MasterEntity=null;
        public BaseEntity MasterEntity
        {
            get
            {
                return _MasterEntity;
            }
        }


        public string JsMasterEntityInstance
        {
            get
            {
                if (MasterEntity == null)
                {
                    if (!(MasterEntity is DataTableBaseEntity))
                    {
                        return ( MasterEntity.ToJSON() );
                    }
                    else
                    {
                        return ((MasterEntity as DataTableBaseEntity).EntityJson);
                    }

                }
                else
                {
                    if (!(MasterEntity is DataTableBaseEntity))
                    {
                        return ( MasterEntity.ToJSON());
                    }
                    else
                    {
                        return ((MasterEntity as DataTableBaseEntity).EntityJson );
                    }
                    //return ("var instance=[" + entity.ToJSON().Replace("\n", "[BR]").Replace("\r", "") + "];");
                }
            }
        }


        /// <summary>
        /// 创建DataTableBaseEntity实例
        /// </summary>
        /// <param name="strEntity">实体或表名</param>
        /// <param name="id">ID的值</param>
        /// <returns></returns>
        public DataTableBaseEntity CreateDTEntity(string strEntity,string id)
        {
            BaseEntity instance = iSun.Common.Globals.getEntity(strEntity) as BaseEntity;
            BaseBussnies bInstance = new iSun.BLL.Base.BaseBussnies(instance);
            BaseEntity entity = null;
            if (instance != null)
            {
                strEntity = instance.GetTableName();
            }
            instance = new DataTableBaseEntity();
            ((DataTableBaseEntity)instance).TableName = strEntity;
            string[] strArrary = ((DataTableBaseEntity)instance).GetKey();
            if (strArrary != null)
            {
                ((DataTableBaseEntity)instance).TableKey = strArrary[0];
                ((DataTableBaseEntity)instance).KeyType = strArrary[1];
            }
            ((DataTableBaseEntity)instance).CreateDT();
            ((DataTableBaseEntity)instance).EntityJson = iSun.Common.Globals.DataRowToJSON(((DataTableBaseEntity)instance).DRow);

            if (id != "") //有ID表示编辑
            {
                entity = new DataTableBaseEntity(); //bInstance.GetEntityById(id) as BaseEntity;
                ((DataTableBaseEntity)entity).TableName = strEntity;
                ((DataTableBaseEntity)entity).TableKey = ((DataTableBaseEntity)instance).TableKey;
                ((DataTableBaseEntity)entity).KeyType = ((DataTableBaseEntity)instance).KeyType;
                ((DataTableBaseEntity)entity).CreateDT(id);
                ((DataTableBaseEntity)entity).EntityJson = iSun.Common.Globals.DataRowToJSON(((DataTableBaseEntity)entity).DRow);
                instance = entity;
            }
            else
            {
                //初始化ID值
                //KeyValue = instance.GetTableIdValue();
                if (KeyValue == Guid.Empty.ToString() || String.IsNullOrEmpty(KeyValue))
                {
                    ((DataTableBaseEntity)instance).DRow[((DataTableBaseEntity)instance).TableKey] = Guid.NewGuid();
                }
                ((DataTableBaseEntity)instance).EntityJson = iSun.Common.Globals.DataRowToJSON(((DataTableBaseEntity)instance).DRow);
                KeyValue = instance.GetTableIdValue();

            }
            return instance as DataTableBaseEntity;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private DataTableBaseEntity GetDTEntity()
        {
            string strEntity = this.GetStringFromQueryString("entity", "");

            string id = this.GetStringFromQueryString("id", "");
            DataTableBaseEntity instance = CreateDTEntity(strEntity, id);
            return instance;
        }



        /// <summary>
        /// 获取实体对象
        /// </summary>
        /// <returns></returns>
        private BaseEntity GetEntity()
        {
            string strEntity = this.GetStringFromQueryString("entity", "");

            string id = this.GetStringFromQueryString("id", "");
            BaseEntity instance = iSun.Common.Globals.getEntity(strEntity) as BaseEntity;
            //MasterEntity = instance;
            BaseBussnies bInstance = new iSun.BLL.Base.BaseBussnies(instance);
            BaseEntity entity = null;

            if (instance == null)
            {
                instance = CreateDTEntity(strEntity, id);
            }
            else
            {
                //初始化ID值
                entity = bInstance.GetEntityById(id) as BaseEntity;
                if (entity != null)
                {
                    instance = entity;
                }
            }

            //获取
            if (instance != null)
            {
                object O = instance.GetKeyFieldValue();
               
                Guid? _id = ((O) == null) ? null : O as Guid?;
                //初始化ID值
                if (_id == null || _id == Guid.Empty)
                {
                    iSun.Common.Globals.SetPropertyValue(instance, instance.GetTableId(), Guid.NewGuid());
                }
            }
            return instance;
        }

        public string JsonData
        {
            get;
            set;
        }

        public string MasterHTML
        {
            get;
            set;
        }

        /// <summary>
        /// 明细表
        /// </summary>
        public string DetailTableHtml
        {
            get;
            set;
        }

        public string KeyValue
        {
            get;
            set;
        }


        public string const_Entity
        {
            get;
            set;
        }
       // public override ent
    }
}